﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TV4Home.Client.WMC.TVClient.UI
{
    /// <summary>
    /// Provides static methods for dealing with the serialization/deserialization of objects.
    /// </summary>
    public static class XML
    {
        #region Public Methods
        /// <summary>
        /// This method deserializes an object from an XML-file.
        /// If the file specified by <paramref name="path"/> doesn't exist, a default instance of the object will be created and written to the file.
        /// </summary>
        /// <typeparam name="T">The type of the object to be deserialized.</typeparam>
        /// <param name="path">The path to the XML-file.</param>
        /// <returns>The object that has been deserialized from the file</returns>
        public static T LoadConfiguration<T>(string path)
        {
            return LoadConfiguration<T>(path, true);
        }

        /// <summary>
        /// This method deserializes an object from an XML-file.
        /// </summary>
        /// <typeparam name="T">The type of the object to be deserialized.</typeparam>
        /// <param name="path">The path to the XML-file.</param>
        /// <param name="createDefault">If set to <c>true</c>, a default instance of the object will be created and written to the file specified by <paramref name="path"/>. Otherwise you will get an exception if the file doesn't exist.</param>
        /// <returns>The object that has been deserialized from the file</returns>
        public static T LoadConfiguration<T>(string path, bool createDefault)
        {
            // create a serializer-object from the given type
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

            // check if the file exists and create a deafult instance of the type if necessary
            if (!System.IO.File.Exists(path) && createDefault)
                WriteConfiguration<T>(path, (T)Activator.CreateInstance(typeof(T)));

            System.Xml.XmlTextReader rd = null;

            try
            {
                rd = new System.Xml.XmlTextReader(path);

                // deserialize the object from the file
                object temp = ser.Deserialize(rd);

                return (T)temp;
            }
            finally
            {
                if (rd != null)
                    rd.Close();
            }
        }

        /// <summary>
        /// This method serializes an object into an XML-file.
        /// </summary>
        /// <typeparam name="T">The type of the object to be serialized</typeparam>
        /// <param name="path">The path to the XML-file.</param>
        /// <param name="config">The object that should be serialized.</param>
        public static void WriteConfiguration<T>(string path, T config)
        {
            // create a serializer-object from the given type
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

            System.Xml.XmlTextWriter wr = null;

            try
            {
                wr = new System.Xml.XmlTextWriter(path, null);
                wr.Formatting = System.Xml.Formatting.Indented;     // indent the XML for easy human reading

                // serialize the object to the file
                ser.Serialize(wr, config);

                wr.Flush();
            }
            finally
            {
                if (wr != null)
                    wr.Close();
            }
        }
        #endregion
    }
}
